﻿using System;
using System.Globalization;
using VisualWip.Core.Interfaces;

namespace VisualWip.Core
{
    public class ZoomManager : IZoomManager
    {
        private readonly Application _application;

        public ZoomManager(Application application)
        {
            _application = application;
        }

        public bool CanZoomOut()
        {
            return _application.ZoomLevel > 25;
        }

        public void ZoomOut()
        {
            _application.ZoomLevel -= 25;
        }

        public bool CanZoomIn()
        {
            return _application.ZoomLevel < 400;
        }

        public void ZoomIn()
        {
            _application.ZoomLevel += 25;
        }

        public void ZoomTo(object o)
        {
            if (!CanZoomTo(o)) return;
            
            var zoomLevelText = o.ToString();
            int zoomLevel;
            if (int.TryParse(zoomLevelText, out zoomLevel))
            {
                _application.ZoomLevel = zoomLevel;
            }
        }

        public void ZoomTo(double scale)
        {
            if(!CanZoomTo(scale)) return;

            _application.ZoomLevel = (int)(scale * 100);
        }

        public bool CanZoomTo(object o)
        {
            var zoomLevelText = o.ToString();
            int zoomLevel;
            if (int.TryParse(zoomLevelText, out zoomLevel))
            {
                return CanZoomTo(zoomLevel);
            }
            return false;
        }

        public bool CanZoomTo(int zoomLevel)
        {
            return zoomLevel > 25 && zoomLevel < 400;
        }

        public bool CanZoomTo(double scale)
        {
            return CanZoomTo((int)(scale*100));
        }

        public int ZoomLevel
        {
            get { return _application.ZoomLevel; }
        }
    }
}